package me.aartikov.sharedelementanimationsample.slice;


import me.aartikov.alligator.Navigator;
import me.aartikov.alligator.animations.AnimationData;
import me.aartikov.alligator.helpers.MiFraction;
import me.aartikov.alligator.annotations.RegisterScreen;
import me.aartikov.sharedelementanimationsample.ResourceTable;
import me.aartikov.sharedelementanimationsample.SampleApplication;
import me.aartikov.sharedelementanimationsample.screens.FirstScreen;
import me.aartikov.sharedelementanimationsample.screens.SecondScreen;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;

import java.util.ArrayList;
import java.util.List;


@RegisterScreen(FirstScreen.class)
public class FirstMiFraction extends MiFraction implements SharedElementProvider {
    List<Image> mKittenImageViews = new ArrayList<>();


    private Navigator mNavigator = SampleApplication.getNavigator();

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_mifraction_first, container, false);
        mKittenImageViews.clear();
        mKittenImageViews.add((Image) component.findComponentById(ResourceTable.Id_kitten_image_view_0));
        mKittenImageViews.add((Image) component.findComponentById(ResourceTable.Id_kitten_image_view_1));
        for (int i = 0; i < mKittenImageViews.size(); i++) {
            int kittenIndex = i;
            mKittenImageViews.get(i).setClickedListener(v -> {
                mNavigator.goForward(new SecondScreen(kittenIndex, getParentId(), getGouptag().toString()), new KittenAnimationData(kittenIndex));
            });
        }
        return component;
    }

    @Override
    protected void setInitialFragmentIfRequired() {

    }


    //
//	@Override
//	public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//		return inflater.inflate(R.layout.fragment_first, container, false);
//	}
//
//	@Override
//	public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//		super.onViewCreated(view, savedInstanceState);
//		mButterKnifeUnbinder = ButterKnife.bind(this, view);
//
//		for (int i = 0; i < mKittenImageViews.length; i++) {
//			int kittenIndex = i;
//			mKittenImageViews[i].setOnClickListener(v -> {
//				mNavigator.goForward(new SecondScreen(kittenIndex), new KittenAnimationData(kittenIndex));
//			});
//		}
//	}

//	@Override
//	public void onDestroyView() {
//		mButterKnifeUnbinder.unbind();
//		super.onDestroyView();
//	}

    @Override
    public Component getSharedElement(AnimationData animationData) {
        KittenAnimationData kittenAnimationData = (KittenAnimationData) animationData;
        return mKittenImageViews.get(kittenAnimationData.getKittenIndex());
    }

    @Override
    public String getSharedElementName(AnimationData animationData) {
        return "kitten";
    }
}
